var wid, uid;
document.addEventListener('plusready',function(){
	wid = plus.webview.currentWebview().id;
	uid = plus.storage.getItem('uid');
	var applytime = plus.storage.getItem('applytime');//申请时间
	var status = plus.storage.getItem('status');//用认证状态
	var authtime = plus.storage.getItem('authtime');//认证成功时间
	var name = plus.storage.getItem('name') != 'null'?plus.storage.getItem('name'):'';//用户名称
	var phone = plus.storage.getItem('phone') != 'null'?plus.storage.getItem('phone'):'';//用户手机号
	var shop = plus.storage.getItem('shop') != 'null'?plus.storage.getItem('shop'):'';//店铺名称
	var address = plus.storage.getItem('address') != 'null'?plus.storage.getItem('address'):'';//店铺地址
	$('.name').val(name);
	$('.phone').val(phone);
	$('.address').val(address);
	$('.shop').val(shop);
	console.log(status);
	if(status == 2 || status == 1){
//		plus.nativeUI.closeWaiting();
//		$('.renzheng').hide();
//		$('.renzhengzhong').show();
//		renzheng();
//	}else if(status == 1){
		plus.nativeUI.closeWaiting();
		$('.renzheng').show();
		$('.renzhengzhong').hide();
		if(plus.storage.getItem('license')){
			$('.license').attr('src',webRoot+plus.storage.getItem('license'));
			//点击图片放大效果
			$('.license').attr('data-preview-src','');
			$('.license').attr('data-preview-group','1');
		}
		if(status == 1){
			renzhengzhong();
			$('.submit').show();
		}else{
			$('.over').hide();
			$('.renzheng input').attr('disabled', true);
			$('.renzheng textarea').attr('disabled', true);
			$('.license').removeClass('uploadPic');
		}
	}else{
		plus.nativeUI.closeWaiting();
		$('.renzheng').show();
		$('.renzhengzhong').hide();
		renzhengzhong();
			$('.submit').show();
	}
	//上传营业执照
	$('.uploadPic').on('tap', function() {
		mui('#sheet1').popover('toggle');
	})
	//选择拍照添加
	$('.paizhao').on('tap', function(){
		var cmr = plus.camera.getCamera(); 
		cmr.captureImage( function(path){
			$('.uploadPic').attr('src',plus.io.convertLocalFileSystemURL(path));
			appendPic('file://'+path);
		}, function(err){}, {index:1} );   
	})
	//选择相册添加
	$('.photo').on('tap', function(){
		plus.gallery.pick( function(path){
		 	$('.uploadPic').attr('src',path);
		 	appendPic(path);
		}, function(error){}, {} );
	})
	var files = {};
	// 添加照片
	function appendPic(p) {
		files = {};
		var newPath = p.replace(/\./g, new Date().getTime() + '.');
		plus.zip.compressImage({
			src: p,
			dst: newPath,
			overwrite: true,
			quality: 20
		},
		function() { //成功回调
			if(_this.attr('cname') != "certificate"){	//医生资格证
				_this.attr('src',newPath);
			}else{
//				_this.before('<img src="'+webRoot+t.responseText+'"/>');
			}
			files.name = "uploadkey";
			files.path = newPath;
			upload();
		},
		function(error) {
			toast('压缩图片失败');
		});
	}
	//创建对像
	function upload(){ 
//					console.log(apiRoot+'?m=Home&c=Member&a=uploadPic');
		var wt = plus.nativeUI.showWaiting();  
		var  task = plus.uploader.createUpload(apiRoot+'?m=Home&c=Member&a=uploadOne',
		{ method:"POST"},
		function ( t, status ) {  
			// 上传完成     
			if ( status == 200 ) { 
				console.log(t.responseText);
				plus.nativeUI.toast("上传成功");    
				$(".uploadPic").attr('src',webRoot+t.responseText);
				plus.storage.setItem("license",t.responseText+""); 
				console.log(plus.storage.getItem('license'));
				wt.close(); 
			} else {
				plus.nativeUI.toast( "上传失败："+status );
				wt.close();
			}  
		});
		task.addFile(files.path,{key:files.name});
		task.start();
	}
},false)
function renzhengzhong(){
	plus.nativeUI.closeWaiting();
	$('.submit').on('click',function(){
		var name = $('.name').val();
		var phone = $('.phone').val();
		var shop = $('.shop').val();
		var address = $('.address').val();
		var license = plus.storage.getItem('license');
		console.log(name+" "+phone+' '+shop+' '+address+' '+license); 
		if(!name || !phone || !shop || !address || !license){
			toast('请完善信息');return;
		}
		showWating();
		$.ajax({
			type:"get",
			url:apiRoot+"?m=home&c=member&a=submitData",
			data :{
				uid :uid,
				name :name,
				phone :phone,
				shop :shop,
				address :address,
				license :license
			},
			dataType :'json',
			success :function(data){
				console.log(JSON.stringify(data));
				if(data > 0){
					plus.storage.setItem('shop', shop+'');
					plus.storage.setItem('shop', shop+'');
					plus.nativeUI.closeWaiting();
					toast('资料提交成功');
					$('.renzheng').hide();
					$('.renzhengzhong').show();
					renzheng();
				}
			},
			error : function(e){
				console.log(JSON.stringify(e));
			}
		});
	})
}
function renzheng(){
//	console.log(apiRoot+"?m=home&c=member&a=authStatus&uid="+uid);
	$.ajax({
		type :"get",
		url :apiRoot+"?m=home&c=member&a=authStatus",
		data :{
			uid :uid
		},
		dataType : 'json',
		success :function(data){
			plus.nativeUI.closeWaiting(); 
			console.log(JSON.stringify(data)); 
			if(data.status >= 1){
				$('.applyInfo').text('您好！您的相关资料正在认证中，敬请耐心等待。');
				$('.applytime').text(data.applytime);
				if(data.status == 2){
					var info = '<div class="progress-li mui-table-view background-ddd">'+
									'<li>'+
										'<span>恭喜您已通过我们的认证!</span>'+
										'<span class="mui-h5">'+data.authtime+'</span>'+
										'<div class="ring"></div>'+
									'</li>'+
								'</div>';
					console.log(info);
					$('.ccc').html(info);
				}
				plus.storage.setItem('applytime',data.applytime+'');//申请时间
				plus.storage.setItem('status',data.status+'');//认证状态
				plus.storage.setItem('authtime',data.authtime+'');//认证成功时间
			}
		},
		error :function(e){
			console.log(JSON.stringify(e));
			errortoast();
		}
	});
}
